import 'package:get/get.dart';
import 'package:vape_xe/routes/app_pages.dart';
import 'package:vape_xe/services/services.dart';
import 'package:vape_xe/utils/utils.dart';

class EnergyController extends GetxController {
  /// 响应式成员变量
  final productList = [].obs;

  late int _type;
  late String _modelNum;

  /// 事件
  fetchNewsList() async {
    await Request().get('/userVape/getList', {
      'type': _type,
      'modelNum': _modelNum,
    }).then((result) {
      productList.value = result['result'];
    });
  }

  void goToUpgrade(int index) {
    if (productList[index]['isMax'] == 1) return;
    Get.toNamed(Routes.UPGRADE, arguments: {'targetId': productList[index]['id']});
  }

  /// 生命周期
  @override
  void onInit() {
    super.onInit();
    _type = Get.arguments['type'];
    _modelNum = Get.arguments['modelNum'];
    ever(EventService.to.message, (message) async {
      if (message.startsWith(EventService.EventRefresh)) {
        fetchNewsList();
      }
    });
  }

  @override
  void onReady() {
    super.onReady();
    fetchNewsList();
  }
}
